package com.zyf.controller;

import com.zyf.common.result.Result;
import com.zyf.common.result.ResultCodeEnum;
import com.zyf.common.util.AuthContextHolder;
import com.zyf.model.user.UserInfo;
import com.zyf.service.UserinfoService;
import com.zyf.vo.user.LoginVo;
import com.zyf.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/** 用户的 控制层类
 * @author zyf
 * @date 2021年05月18日 11:34
 */
@Api(value = "用户管理的接口")
@RestController
@RequestMapping("/admin/user")
public class UserinfoController {

    @Autowired
    private UserinfoService userinfoService;

    @ApiOperation(value = "用户手机号登录")
    @PostMapping("/login")
    @ApiImplicitParam(name = "loginVo", value = "用户登录VO类")
    public Result login(@RequestBody LoginVo loginVo){
        Map<String,Object> map = userinfoService.loginUser(loginVo);
        Object code = map.get("code");
        if (code == ResultCodeEnum.REGISTER_WEIXIN_ERROR){
            return Result.fail().message(ResultCodeEnum.REGISTER_WEIXIN_ERROR.getMessage());
        }
        return Result.ok(map);
    }

    @ApiOperation(value = "用户认证Api")
    @PostMapping("/auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request){
        //传递两个参数，第一个参数用户id，第二个参数用户认证数据vo对象
        userinfoService.userAuth(AuthContextHolder.getUserId(request),userAuthVo);
        return Result.ok();
    }



    @ApiOperation(value = "获取用户id信息Api")
    @GetMapping("/auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userinfoService.getById(userId);
        return Result.ok(userInfo);
    }

    @ApiOperation(value = "手机号重新绑定微信")
    @PostMapping("/rebinding")
    public Result rebinding(@RequestBody LoginVo loginVo){
        Map<String,Object> map = userinfoService.rebinding(loginVo);
        return Result.ok(map);
    }

    @ApiOperation(value = "删除过期用户")
    @DeleteMapping("/deleteUserFile")
    public Result deleteUserFile(@RequestBody LoginVo loginVo){
        userinfoService.deleteUserFile(loginVo);
        return Result.ok();
    }

}
